package com.qf.web_last.Controller;

import com.qf.web_last.Entirty.User;
import com.qf.web_last.Entirty.UserDTO;
import com.qf.web_last.Service.UserService;
import com.qf.web_last.Until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 宝剑锋自磨砺出，梅花香自苦寒来
 *
 * @Author:mchao
 * @Time:2025/9/29
 */
@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:8080")
public class UserController {

    @Autowired
    private UserService userService;

    //登录接口
    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO) {

        System.out.println("前端传递的 userDTO：" + userDTO);
        System.out.println("前端传递的 username：" + userDTO.getUsername());
        System.out.println("前端传递的 password：" + userDTO.getPassword());

        User user = userService.login( userDTO);
        System.out.println(user);
        if (user != null) {
            return Result.success(user);
        } else {
            return Result.error("用户名或密码错误");
        }

    }


    //注册接口
    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO) {
        return userService.register(userDTO) ? Result.success() : Result.error("用户已存在");
    }


    //根据用户名查询用户
    @GetMapping("/getUser")
    public Result getUserByUsername(String  username) {
       int i = userService.getUserByUsername(username);
       if (i>0){
           return Result.success();
       }else {
           return Result.error("用户不存在");
       }
    }

    @GetMapping("/getStudents")
    public Result getStudents(){
        return Result.success(userService.getStudents());
    }


    @PostMapping("/addStudent") // 接口路径需与前端一致
    public Result addStudent(@RequestBody User user) {
        // 处理添加学生的业务逻辑
        // student对象会自动接收前端传来的username和sex参数
        System.out.println("姓名: " + user.getUsername());
        System.out.println("性别编码: " + user.getSex()); // 1-男，0-女
        System.out.println("手机号: " + user.getPhone());
        System.out.println("钱: " + user.getMoney());

        // 业务处理...
        boolean success = userService.addStudent(user);
        if (!success) {
            return Result.error("添加失败");
        }else {
            return Result.success("添加成功");
        }
    }


    @PostMapping("/updateStudent")
    public Result updateStudent(@RequestBody User user) {
        // 业务处理...
        boolean success = userService.updateStudent(user);
        if (!success) {
            return Result.error("更新失败");
        }else {
            return Result.success("更新成功");
        }
    }

    //根据id删除学生
    @PostMapping("/deleteStudent")
    public Result deleteStudent(@RequestBody User  user) {
        // 业务处理...
        boolean success = userService.deleteStudent(user);
        if (!success) {
            return Result.error("删除失败");
        }else {
            return Result.success("删除成功");
        }
    }


    @PostMapping("/searchStudents")
    public Result searchStudent(@RequestBody User  user) {
        System.out.println("姓名: " + user.getUsername());
        System.out.println("性别编码: " + user.getSex());
        // 业务处理...
        List<User> students = userService.searchStudent(user);
        if (students.isEmpty()) {
            return Result.error("没有找到该学生");
        }else {
            return Result.success(students);
        }
    }


    //根据性别获取学生数量(0/1/其他)
    @GetMapping("/getStuNumber")
// 添加 @RequestParam(required = false) 允许 sex 参数为空（对应“未定义”场景）
    public Result getStuNumber(@RequestParam(required = false) Integer sex) {
        int count;
        // 明确逻辑：sex为null或非0/1时，查询“未定义”的数量
        if (sex == null || (sex != 0 && sex != 1)) {
            count = userService.getStuNumber(null); // 假设Service层支持null查询未定义
        } else {
            count = userService.getStuNumber(sex);
        }
        return Result.success(count); // 后端返回的data是数字（如5、10）
    }

}
